package pattern.command;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * 小猪吃饭命令模式
 *
 * @author sun.ye
 * @since 2020-07-14 19:16
 */
@Slf4j
@Data
public class PigEatCommand implements EatCommand {

    private String name;
    private int appetite;
    private int nowEat;
    private boolean full;

    public PigEatCommand(String name, int appetite) {
        this.name = name;
        this.appetite = appetite;
        this.full = false;
    }

    @Override
    public void eat() {
        Random random = new Random(System.currentTimeMillis());
        int food = random.nextInt(25);
        nowEat += food;
        log.info("{} can eat {},this time eat {},nowEat {}",name,appetite,food,nowEat);
        if (nowEat >= appetite){
            full = true;
        }
    }

    @Override
    public boolean full() {
        if (full){
            log.info(name + " is full");
        }
        return full;
    }
}